
package com.walkingfiles.pubsub

class PubSub {
    
    static def listeners = [:]

    static def sub(String topic, callback) {
        
        if (PubSub.listeners[topic] == null) {
            PubSub.listeners[topic] = []
        }

        def topicListeners = PubSub.listeners[topic]
        topicListeners << callback
        
    }

    static def pub(String topic, object) {
        def topicListeners = PubSub.listeners[topic] 
        if (topicListeners == null) {
            return
        }
        topicListeners.each { callback -> 
            callback(object)
        }        
    }

}

